﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace GameStudio
{
    class GenerateCode 
    {
        private string m_strFileName;
        private string m_Suffix = ".cs";
        private string NewNameSpace;
        private string m_Path;
        StreamWriter sw;
        Stream s;
        public GenerateCode(string strFileName, string Path, string NameSpace)
        {
            m_strFileName = strFileName;
            NewNameSpace = NameSpace;
            m_Path = Path;

        }

        public void CreateCode()
        {
            string pth = System.IO.Path.Combine(m_Path, m_strFileName);
            s = File.Open(pth + m_Suffix, FileMode.Create);
            sw = new StreamWriter(s);

            CSharpCodeProvider cscProvider = new CSharpCodeProvider();
            ICodeGenerator cscg = cscProvider.CreateGenerator(sw);
            CodeGeneratorOptions cop = new CodeGeneratorOptions();
            //Create Class Using Statements
            CodeSnippetCompileUnit csu1 = new CodeSnippetCompileUnit("using System;");
            CodeSnippetCompileUnit csu2 = new CodeSnippetCompileUnit("using System.IO;");
            cscg.GenerateCodeFromCompileUnit(csu1, sw, cop);
            cscg.GenerateCodeFromCompileUnit(csu2, sw, cop);
            sw.WriteLine();

            //Create Class Namespaces
            CodeNamespace cnsCodeDom = new CodeNamespace(NewNameSpace);

            //Create Class Declaration

            CodeTypeDeclaration ctd = new CodeTypeDeclaration(m_strFileName);
            ctd.IsClass = true;
            //ctd.Name = m_strFileName;
            ctd.TypeAttributes = TypeAttributes.Public;
            //ctd.Members.Add(

            //Create Class Constructor				
            sw.WriteLine();

            CodeConstructor ccon = new CodeConstructor();
            ccon.Attributes = MemberAttributes.Public;
            ccon.Statements.Add(new CodeSnippetStatement("//"));
            ccon.Statements.Add(new CodeSnippetStatement("// TODO: Add constructor logic here"));
            ccon.Statements.Add(new CodeSnippetStatement("//"));
            ctd.Members.Add(ccon);

            cnsCodeDom.Types.Add(ctd);

            //Generate Source Code File
            cscg.GenerateCodeFromNamespace(cnsCodeDom, sw, cop);

            //Close StreamWriter
            sw.Close();
            s.Close();
        }

    }
}
